<?php

/**
 * @author nhatnam
 * @copyright 2011
 */
include ('clsketnoi.php');

function taomadausach()
{
    return taomatudong("select madausach from tbldausach", "madausach", 3, 14);
}
function uploadfilesach()
{
    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/pjpeg"))
    && ($_FILES["file"]["size"] < 2000000))
    {
      if ($_FILES["file"]["error"] > 0)
        {
        //echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        }
      else
        {
        //echo "Upload: " . $_FILES["file"]["name"] . "<br />";
        //echo "Type: " . $_FILES["file"]["type"] . "<br />";
        //echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        //echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
    
        if (file_exists("../upload/hinhsach/" . $_FILES["file"]["name"]))
          {
          //echo $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
          move_uploaded_file($_FILES["file"]["tmp_name"],
          "../upload/hinhsach/" . $_FILES["file"]["name"]);
          
          //echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
          }
        }
    }
    else
    {
      //echo "Invalid file";
      
    }
}
//them du lieu
function themdausach()
{
    $mads= $_POST['mads'];
    $tensach =$_POST['tensach'];
    $tomtat =$_POST['tomtat'];
    $giabia =$_POST['giabia'];
    $soluong = $_POST['soluong'];
    $namxb = $_POST['namxb'];
    $hinh = $_FILES['file']['name'];
    $matg = $_POST['matg'];
    $matl = $_POST['matl'];
    $manv = $_POST['manv'];
    $manxb = $_POST['manxb'];

    mysql_query("insert into tbldausach (madausach, matg, matl, manxb, manv, hinh, ten, tomtat, giabia, soluong, namxb)".
    " values('$mads','$matg','$matl','$manxb','$manv','$hinh','$tensach','$tomtat','$giabia','$soluong','$namxb')",dbketnoi());

    mysql_free_result();
}
//xoa dau sach
function xoadausach()
{
    $mads= $_POST["mads"];
    $query = mysql_query("select madausach, macuonsach from tblcuonsach",dbketnoi());
    $i=0;
    while($result = mysql_fetch_array($query))
    {
        if($result['madausach']==$mads)
        {
            mysql_query("delete from tblcuonsach where macuonsach='".$result['macuonsach']."'", dbketnoi());
        }
        $i++;
    }
       
    mysql_query("delete from tbldausach where madausach='$mads'",dbketnoi());
    mysql_free_result();
}
//update dau sach
function updatedausach()
{
    $mads= $_POST['mads'];
    $tensach =$_POST['tensach'];
    $tomtat =$_POST['tomtat'];
    $giabia =$_POST['giabia'];
    $soluong = $_POST['soluong'];
    $namxb = $_POST['namxb'];
    $hinh = $_FILES['file']['name'];
    $matg = $_POST['matg'];
    $matl = $_POST['matl'];
    $manv = $_POST['manv'];
    $manxb = $_POST['manxb'];
    
    mysql_query("update tbldausach set matg='$matg', matl='$matl', manxb='$manxb', manv='$manv', hinh='$hinh', ten='$tensach',".
    " tomtat='$tomtat', giabia='$giabia', soluong='$soluong', namxb='$namxb' where madausach='$mads'",dbketnoi());
    mysql_free_result();
}

// hien thi du lieu
function hienthidausach()
{
    $limit = 15; //gioi han so row trong 1 trang

    $page = isset ( $_GET["page"] ) ? intval ( $_GET["page"] ) : 1; //get trang hien thi

    $page_start = ( $page - 1 ) * $limit;
    $page_end = $page * $limit;

    $query = mysql_query("SELECT madausach, ten, tbltacgia.hoten as tentacgia, tentl, tennxb, namxb, tblnhanvien.hoten as tennhanvien, giabia, soluong ".
    " from tbldausach, tblnhanvien, tblnhaxuatban, tbltacgia, tbltheloai where tbldausach.manv=tblnhanvien.manv and tbldausach.matl=tbltheloai.matl ".
    " and tbldausach.matg=tbltacgia.matg and tbldausach.manxb=tblnhaxuatban.manxb",dbketnoi());

    $number_of_page = ceil(mysql_num_rows($query)/$limit); // lay tong row

    if ($number_of_page > 1)
    {
        $list_page = " <td> Trang: </td>";

        for ( $i = 1; $i <= $number_of_page; $i++ )
            {
            if ( $i == $page )
            {
                $list_page .= " <td>[ <b>{$i}</b> ]</td> ";
           
            }
            else
            {
                $list_page .= "<td><a href='giaodiendausach.php?page={$i}'> {$i} </a></td>";
                
            }
        }
    }
    taotable();
    $i = 0;
    while ( $result = mysql_fetch_array ( $query ) )
    {
        if ( $i >= $page_start )
        {
            //mau bg cho dong chan le
            if($i%2==0)
                $bg = "#d0eafb";
            else
                $bg = "#66aee0";
            
            //tao ra 1 table bang lenh ket hop css
            echo "<tr bgcolor='".$bg."'>"; 
            echo "<td><a href='giaodiendausach.php?mads=".$result['madausach']."'>".$result['madausach']."</a></td>";
            echo "<td>".$result['ten']."</td>";
            echo "<td>".$result['tentacgia']."</td>";
            echo "<td>".$result['tentl']."</td>";
            echo "<td>".$result['tennxb']."</td>";
            echo "<td>".$result['namxb']."</td>";
            echo "<td>".$result['tennhanvien']."</td>";
            echo "<td>".$result['giabia']."</td>";
            echo "<td>".$result['soluong']."</td>";
            echo "</tr>";
        }

        $i++;

        if ($i >= $page_end)
        {
            break;
        }
    }
    echo "</table></br>";
    echo $list_page;
}
function taotable()
{
    $title = array("Mã ĐS ","Tên ĐS","Tác Giả","Thể Loại", "Tên NXB", "Năm XB", "Nhân Viên", "Giá Bìa", "Số Lượng");
    echo "<table style='border-color:black;'  cellspacing='1' width='900'>";
    echo "<tr>";
    for($i=0; $i<=8; $i++)
    {
        echo "<th style='background-color:#2287cd; color='white';>".$title[$i]."</th>"; 
    }
    echo "</tr>";
  
}
// hien thị dropdownlist
function hienthinhaxuatban()
{
    $query = mysql_query("select manxb, tennxb from tblnhaxuatban",dbketnoi());
    $i=0;
    echo "<select size='1' name='manxb'>";
    echo "<option value=''>----Chọn----</option>";
    while($result = mysql_fetch_array($query) )
    {
        echo "<option value='".$result['manxb']."'>".$result['tennxb']."</option>";
        $i++;
    }
    echo "</select>";
}
function hienthitacgia()
{
    $query = mysql_query("select matg, hoten from tbltacgia",dbketnoi());
    $i=0;
    echo "<select size='1' name='matg'>";
    echo "<option value=''>----Chọn----</option>";
    while($result = mysql_fetch_array($query) )
    {
        echo "<option value='".$result['matg']."'>".$result['hoten']."</option>";
        $i++;
    }
    echo "</select>";
}

function hienthitheloai()
{
    $query = mysql_query("select matl, tentl from tbltheloai",dbketnoi());
    $i=0;
    echo "<select size='1' name='matl'>";
    echo "<option value=''>----Chọn----</option>";
    while($result = mysql_fetch_array($query) )
    {
        echo "<option value='".$result['matl']."'>".$result['tentl']."</option>";
        $i++;
    }
    echo "</select>";
}
function hienthinhanvien()
{
    $query = mysql_query("select manv, hoten, bophan from tblnhanvien where bophan='Thủ kho'",dbketnoi());
    $i=0;
    echo "<select size='1' name='manv'>";
    echo "<option value=''>----Chọn----</option>";
    while($result = mysql_fetch_array($query) )
    {
        echo "<option value='".$result['manv']."'>".$result['hoten']."</option>";
        $i++;
    }
    echo "</select>";
}

function thongtindausach()
{
    $mads = $_GET['mads'];
    $query = mysql_query("select madausach, hinh, ten, giabia from tbldausach where madausach='$mads'", dbketnoi());
    $i = 0;
    while ( $result = mysql_fetch_array ( $query ) )
    {
        
           echo "
            <table>
                <tr>
                    <td><img height='150' width='110' src='upload/hinhsach/".$result['hinh']."'/></td>
                </tr>
                <tr>
                    <td>Tên Đầu Sách: ".$result['ten']."</td>
                </tr>
                <tr>
                    <td>Giá Bìa: ".$result['giabia']." VNĐ</td>
                </tr>
            </table>
        
            </div>";
       
        $i++;
    }
}
?>